<?php
	//$_SESSION['cart'][$_CFG[pos][default_coperto]] = 1;
	
		/*
		<script type="text/javascript" src="./fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
		<script type="text/javascript" src="./fancybox/jquery.fancybox-1.3.4.pack.js"></script>
		<link rel="stylesheet" type="text/css" href="./fancybox/jquery.fancybox-1.3.4.css" media="screen" />
		*/
?>
		<fieldset>
		<legend>Cassa</legend>
		<div id="erasecart" class="clear"><img style="vertical-align: middle" src="images/cart_del_18x18.png" alt="Cancella Carrello" /> Cancella Carrello</div>
		<script type="text/javascript">$('#erasecart').click(function(){$('#cassa').load('ajax.php?case=cassa&erase=1')})</script>
		<?php
		/*dalla sessione carrello carica dati prodotti dal db e li mette in un array*/
		if(isset($_SESSION['cart']))
		{
			foreach ($_SESSION['cart'] as $key => $value)
			{
				$ids .= ", $key";
			}

			$query = "select * from prodotti where IDprodotto in(0".$ids."); ";
			if ($result = $mysqli->query($query))
			{
				while ($row = $result->fetch_assoc())
				{
					$prodotti[$row[IDprodotto]]=$row;
				}
			}
		}

		/* debug */
		if($_CFG[debug])
		{
			echo '<textarea rows="10" cols="40">';
			echo $query."\n";
			if(isset($_SESSION['cart']))
			{
				echo "Tavolo: ".$_SESSION['cart-tavolo']."\n";
				echo "Cliente: ".$_SESSION['cart-cliente']."\n";
				foreach ($_SESSION['cart'] as $key => $value)
				{
					echo "ID:".$key." | Q.ta':".$value."\n";
				}
			}
			echo "\n";
			print_r($prodotti);
			echo '</textarea>';
		}
		/* visualizza la testa del carrello */
		if(isset($_SESSION['cart-tavolo']) || isset($_SESSION['cart-cliente']))
		{
			echo "<div id=\"carthead\" class=\"riga\"><em>Tavolo:</em> ".$_SESSION['cart-tavolo']." - <em>Cliente:</em> ".$_SESSION['cart-cliente']."</div>";
		}
		/* visalizza le righe del carrello */
		if(isset($_SESSION['cart']))
		{
			$cart_tot = 0;
			foreach ($_SESSION['cart'] as $key => $value)
			{
				$cart_rig = $value*$prodotti[$key][prezzo];
				$cart_tot += $cart_rig;
				$idview = ($_CFG[debug])? "\t".$prodotti[$key][IDprodotto].' ['.$prodotti[$key][IDcategoria].'] '."\n" : '' ;
				echo "\n<div class=\"riga\" id=\"cas-$key\">\n"
					."\t".$idview.$prodotti[$key][prodotto]."\n"
					."\t<span class=\"qtaa right\">\n\t\t Q.ta':\n"
				//	."\t\t<img id=\"cart-add-$key\" src=\"images/cart_add_18x18.png\"/>\n"
					."\t\t<button class=\"button green dot\" id=\"cart-add-$key\">+</button>\n"
					."\t\t<input id=\"cart-qta-$key\" class=\"qta\" type=\"text\" value=\"$value\" />\n"
				//	."\t\t<img id=\"cart-sub-$key\" src=\"images/cart_sub_18x18.png\"/>\n"
					."\t\t<button class=\"button red dot\" id=\"cart-sub-$key\">-</button>\n"
					."\t\t &euro;".number_format($cart_rig,2,',','')."\n"
				//	."\t\t<img id=\"cart-rem-$key\" src=\"images/cart_rem_18x18.png\"/>\n"
					."\t\t<button class=\"button black dot\" id=\"cart-rem-$key\" style=\"color:red;\">X</button>\n"
					."\t</span>\n</div>\n"
					."<script type='text/javascript'>\$('#cart-add-".$prodotti[$key][IDprodotto]."').click(function(){\$('#cassa').load('ajax.php?case=cart-add&id=".$prodotti[$key][IDprodotto]."')})</script>\n"
					."<script type='text/javascript'>\$('#cart-qta-".$prodotti[$key][IDprodotto]."').change(function(){\$('#cassa').load('ajax.php?case=cart-qta&id=".$prodotti[$key][IDprodotto]."&val='+$(this).val())})</script>\n"
					."<script type='text/javascript'>\$('#cart-sub-".$prodotti[$key][IDprodotto]."').click(function(){\$('#cassa').load('ajax.php?case=cart-sub&id=".$prodotti[$key][IDprodotto]."')})</script>\n"
					."<script type='text/javascript'>\$('#cart-rem-".$prodotti[$key][IDprodotto]."').click(function(){\$('#cassa').load('ajax.php?case=cart-rem&id=".$prodotti[$key][IDprodotto]."')})</script>\n";
			}
			echo "<div id=\"cart-tot\">"
				."\t<span class=\"right\"><input id=\"cart-totale\" class=\"collapse\" type=\"text\" value=\"$cart_tot\" /><strong>TOTALE &euro;".number_format($cart_tot,2,',','')."</strong></span><br />"
				."\t<span class=\"right\">Contanti &euro; <input id=\"cart-contanti\" class=\"contanti\" type=\"text\" value=\"0\" /></span><br />"
				."\t<script type='text/javascript'>\$('#cart-contanti').change(function(){\$('#cart-resto').load('ajax.php?case=cart-resto&tot='+$('#cart-totale').val()+'&cont='+$(this).val().replace(',', '.'))})</script>\n"
				."\t<br /><span id=\"cart-resto\" class=\"right\">RESTO &euro;".number_format($cart_tot,2,',','')."</span><br />"
				."</div>\n";
		}
		if(isset($_SESSION['cart-cliente']))
		{
		?>		
		<button id="salva" class="button large gray">SALVA</button>
		<?php if($_CFG[debug]){ ?>
		<script type='text/javascript'>$('#salva').click(function(){$('#cassa').load('ajax.php?case=stampa')})</script>
		<?php }else{ ?>
		<script type='text/javascript'>$('#salva').click(function(){$('#cassa').load('ajax.php?case=stampa', location.reload())})</script>
		<?php } ?>
		<button id="stampa" class="button large green" href="stampa.php">STAMPA</button>
		<script type='text/javascript'>
		$("#stampa").fancybox({
				'width'				: '100%',
				'height'			: '100%',
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe',
				'onClosed': function() {
					parent.location.reload(true);
				}
		});
		</script>
		<?php } ?>
		</fieldset>
